

import com.sun.net.httpserver.Headers;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import fr.paris8.iut.info.iddl.IDDLReasoner;
import fr.paris8.iut.info.iddl.conf.Semantics;
import java.net.URI;
import java.util.concurrent.Executors;

public class Main {

   public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(8080);
    HttpServer server = HttpServer.create(addr, 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(Executors.newCachedThreadPool());

    server.createContext("/Index", new IndexHandler());
    server.setExecutor(Executors.newCachedThreadPool());

    server.createContext("/IDDL", new IDDLHandler());
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();

    System.out.println("Server is listening on port 8080" );

   
  }
}

class MyHandler implements HttpHandler {
    static final String HTML_START =
             "<html>" +
             "<title>IDDL reasoner</title>" +
             "<body>" +
             "<font size=6>IUT de Montreuil - Laboratoire LINC</font><hr /></br>" +
             "";

     static final String HTML_END =
             "</body>" +
             "</html>";
  public void handle(HttpExchange exchange) throws IOException {
    String requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("GET")) {
      Headers responseHeaders = exchange.getResponseHeaders();
      responseHeaders.set("Content-Type", "text/html");

      String s = MyHandler.HTML_START+"<center>IDDL reasoner</br><a href='/Index'>Access</a></br>(C) LINC, 2011-2012</center>"+MyHandler.HTML_END;

      exchange.sendResponseHeaders(200,s.length());

      OutputStream responseBody = exchange.getResponseBody();
      responseBody.write(s.getBytes());
      responseBody.close();
    }
  }
}

class IndexHandler implements HttpHandler {
    static final String HTML_START =
             "<html>" +
             "<title>IDDL reasoner</title>" +
             "<body>"+
             "<font size=6>IUT de Montreuil - Laboratoire LINC</font><hr /></br>"
             ;

     static final String HTML_END =
             "</body>" +
             "</html>";
  public void handle(HttpExchange exchange) throws IOException {
    //String requestMethod = exchange.getRequestMethod();
    OutputStream responseBody = exchange.getResponseBody();
    String requestMethod = exchange.getRequestMethod();

      if (requestMethod.equalsIgnoreCase("GET")) {

                      // The default home page
                      String responseString = IndexHandler.HTML_START +
                      "<form action=\"/IDDL\" enctype=\"multipart/form-data\"" +
                      "method=\"get\"><table><tr>" +
                      "<td>Ontology 1:</td><td><input name=\"onto1\" type=\"text\" style='width:530px'></td></tr>" +
                      "<tr><td>Ontology 2:</td><td><input name=\"onto2\" type=\"text\" style='width:530px'></td></tr>" +
                      "<tr><td>Alignment:</td><td><input name=\"Align\" type=\"text\" style='width:530px'></td></tr></table>" +
                     "<input value=\"Check\" type=\"submit\"></form>" +                      
                     IndexHandler.HTML_END;
                  exchange.sendResponseHeaders(200,responseString.length());


                  responseBody.write(responseString.getBytes());
                  responseBody.close();

         }
    
  }
}

class IDDLHandler implements HttpHandler {

    static final String HTML_START =
             "<html>" +
             "<title>IDDL reasoner</title>" +
             "<body>"+
             "<font size=6>IUT de Montreuil - Laboratoire LINC</font><hr /></br>"
             ;

     static final String HTML_END =
             "</body>" +
             "</html>";
  public void handle(HttpExchange exchange) throws IOException {

      
      
    String requestMethod = exchange.getRequestMethod();
    if (requestMethod.equalsIgnoreCase("GET")) {
      Headers responseHeaders = exchange.getResponseHeaders();
      responseHeaders.set("Content-Type", "text/html");

      URI queryString = exchange.getRequestURI();

      String query = queryString.getQuery();

      String[] temp = query.split("&");
      String[] ontolo1 = temp[0].split("=");
      String[] ontolo2 = temp[1].split("=");
      String[] alignment = temp[2].split("=");
      
      String onto1=ontolo1[1];//"http://www.inrialpes.fr/exmo/people/cleduc/geography.owl";
      String onto2 = ontolo2[1]; //"http://www.inrialpes.fr/exmo/people/cleduc/geopolitics.owl";
      String align = alignment[1];//"http://aserv.inrialpes.fr:8089/alid/1349534542747/181";

      String s = IDDLHandler.HTML_START + "The network of ontology:</br>Ontology 1:"+onto1+
              "</br>Ontology 2:"+onto2+"</br>Alignement:"+align+"</br> "
              ;


      IDDLReasoner reasoner = new IDDLReasoner();
    //check IDDL consistency
    reasoner = new IDDLReasoner();
    reasoner.addOntology(URI.create(onto1));
    reasoner.addOntology(URI.create(onto2));
    reasoner.addAlignment(URI.create(align));

    reasoner.setSemantics(Semantics.IDDL);
    if (reasoner.isConsistent()) {

        s+= "<b>is consistent</b>"+IDDLHandler.HTML_END;
    } else
        s += "<b>is inconsistent</b>"+IDDLHandler.HTML_END;

      exchange.sendResponseHeaders(200,s.length());

      OutputStream responseBody = exchange.getResponseBody();
      responseBody.write(s.getBytes());
      responseBody.close();
  }
  }
}


  









